package com.market.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.entity.SendBack;
import com.market.service.SendBackService;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class SendBackController {
    @Autowired
    private SendBackService sendBackService;

    @RequestMapping("findSendBack")
    public R findSendBack(Integer buyerId, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<SendBack> sendBackRecords = sendBackService.findSendBackRecords(buyerId);
        if (sendBackRecords == null) {
            return new R(304, "查找退货记录失败", null);
        }
        return R.querySuccess(new PageInfo<>(sendBackRecords));
    }

    @RequestMapping("addSendBack")
    public R addSendBack(@RequestBody SendBack sendBack) {
        boolean record = sendBackService.addSendBackRecord(sendBack);
        if (!record) {
            return new R(305, "增加退货记录失败", null);
        }
        return new R(205, "增加退货记录成功", null);
    }

    @RequestMapping("modifySendBackState")
    public R modifySendBackState(int orderId, int state) {
        boolean sendBackState = sendBackService.modifySendBackState(orderId, state);
        if (!sendBackState) {
            return new R(306, "修改退货订单状态失败", null);
        }
        return new R(206, "修改退货订单状态成功", null);
    }

    //查看全部退货单
    @GetMapping("queryAllSendBack")
    public R queryAllSendBack(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<SendBack> sendBacks = sendBackService.queryAllSendBack();
        if (sendBacks.size() != 0) {
            return new R(307, "查询全部退货单成功",new PageInfo<>(sendBacks));
        }
        return new R(207, "查询全部退货单失败");
    }
}
